/**   
 * @Title: OrderController.java
 * @Package com.wx.controller
 * @Description: 
 * @author wangxiao
 * @date 2017年11月8日 下午2:53:36
 */
package com.wx.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.eova.common.utils.xx;
import com.eova.interceptor.AuthInterceptor;
import com.eova.interceptor.LoginInterceptor;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.MenuApi;

/**   
 * @ClassName: OrderController
 * @Description: 
 * @author wangxiao
 * @date 2017年11月8日 下午2:53:36
 */
@Clear({LoginInterceptor.class,AuthInterceptor.class})
public class OrderController22 extends Controller{
	static Log log = Log.getLog(OrderController22.class);
	public void index(){
		renderTemplate("/wx/xieyi.html");
	}
	public void test(){
		String str = getPara("JSONDATA");
		try {
			str = URLDecoder.decode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.info(str);
	}
	
	/**
	 * 创建菜单
	 */
	public void createMenu()
	{
		String str = "{\n" +
				"    \"button\": [\n" +
				"        {\n" +
				"            \"name\": \"行包寄件\",\n" +
				"            \"url\": \""+xx.getConfig("domain")+"/oauth/toOauth?appid=wx3bf19919bcd6fc46\",\n" +
				"            \"type\": \"view\"\n" +
				"        },\n" +
				"        {\n" +
				"            \"name\": \"安全保障\",\n" +
				"            \"key\": \"112\",\n" +
				"\t    \"type\": \"click\"\n" +
				"        },\n" +
				"        {\n" +
				"\t    \"name\": \"查询轨迹\",\n" +
				"\t    \"url\": \"http://m.bajie8.com/footer/cjwt\",\n" +
				"\t    \"type\": \"view\"\n" +
				"        }\n" +
				"    ]\n" +
				"}";
		ApiResult apiResult = MenuApi.createMenu(str);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());
	}
	
	public void getProCity() {
//		StringBuffer sql = new StringBuffer();
//		sql.append("SELECT                                                ");
//		sql.append("	dp.PROVINCE_ID id,                                     ");
//		sql.append("	dp.PROVINCE_NAME NAME,                                    ");
//		sql.append("	'-1' parentId,                                      ");
//		sql.append("	'' shortName,                                       ");
//		sql.append("	'' letter,                                          ");
//		sql.append("	'' cityCode,                                        ");
//		sql.append("	'' pinyin                                           ");
//		sql.append("FROM                                                  ");
//		sql.append("	d_province dp                                       ");
//		sql.append("WHERE                                                 ");
//		sql.append("	dp.COMPANY_ID = '54a1f59b07ca11e7b74a5254000c718e'  ");
//		sql.append("UNION ALL                                             ");
//		sql.append("	SELECT                                              ");
//		sql.append("		dc.CITY_ID id,                                    ");
//		sql.append("		dc.CITY_NAME NAME,                                ");
//		sql.append("		dc.PROVINCE_ID parentId,                          ");
//		sql.append("		dc.CITY_SHORT shortName,                          ");
//		sql.append("		'' letter,                                        ");
//		sql.append("		'' cityCode,                                      ");
//		sql.append("		dc.CITY_PINYIN pinyin                             ");
//		sql.append("	FROM                                                ");
//		sql.append("		d_province dp,                                    ");
//		sql.append("		d_city dc                                         ");
//		sql.append("	WHERE                                               ");
//		sql.append("		dp.COMPANY_ID = '54a1f59b07ca11e7b74a5254000c718e'");
//		sql.append("	AND dp.PROVINCE_ID = dc.PROVINCE_ID                 ");
//		List<Record> rList = Db.find(sql.toString());
//		List arr = new ArrayList<>();
//		for(Record r :rList){
//			Map m = JSONUtil.parseObj(r.toJson());
//			System.out.println(m);
//			arr.add(m);
//		}
		
//		String str = JSONUtil.toJsonStr(arr);
//		System.out.println(str);
		StringBuffer sqls = new StringBuffer("[");
		String name = "";
		Map map = new HashMap<>();
		List<Record> rList = Db.find("select dp.PROVINCE_ID,dp.PROVINCE_NAME P,1 sort   from d_province dp where dp.COMPANY_ID = '54a1f59b07ca11e7b74a5254000c718e'" );
		JSONArray json= new JSONArray();
		for(Record r :rList){
			Map proMap = new HashMap<>();
//			proMap.put("Code", r.get("1"));
			proMap.put("p", r.get("P"));
			List<Record> cityList = Db.find("select dc.city_id,dc.city_name C,1 sort   from d_city dc where dc.PROVINCE_ID = '"+r.get("PROVINCE_ID")+"'" );
			List<Map> list = new ArrayList<>();
			for(Record rr :cityList){
//				if(!"乌鲁木齐".equals(rr.get("city_name"))){
//					continue;
//				}
				Map map1 = new HashMap<>();
//				map1.put("Code", rr.get("1"));
				map1.put("n", rr.get("C"));
//				map1.put("sort", 1);
//				List<Record> dList = Db.find("select d.DISTRICT_ID,d.DISTRICT_NAME from d_district d where d.CITY_ID='"+rr.getStr("city_id")+"'" );
//				List<Map> list2 = new ArrayList<>();
//				for(Record rrr :dList){
//					Map map2 = new HashMap<>();
//					map2.put("Code", rrr.get("1"));
//					map2.put("Name", rrr.get("DISTRICT_NAME"));
//					map2.put("sort", 1);
//					list2.add(map2);
//				}
//				map1.put("level", list2);
				list.add(map1);
			}
			proMap.put("c",list);
			json.add(proMap);
		}
		log.info(json.toJSONString());
//		for(Menu m : menuList){
//			Map<String,Object> map = new HashMap<String,Object>();
//			map.put("id", m.getId());
//			map.put("code", m.getCode());
//			map.put("parentId", m.getParentId());
//			map.put("menuName", m.getMenuName());
//			map.put("menuUrl", m.getMenuUrl());
//			map.put("sortId", m.getSortId());
//			map.put("target", "treeFrame");
//			map.put("isHasMenu", true);
//			jsonArray.add(map);
//		}
//		return jsonArray;
	}
}